package test;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.LinkedList;

import junit.framework.TestCase;

import org.codehaus.groovy.control.CompilerConfiguration;

public class T extends TestCase {
	public void test1(){
		
		Binding binding = new Binding();
		CompilerConfiguration config = new CompilerConfiguration();
		GroovyShell shell = new GroovyShell(binding, config);
		
        PrintStream oldPrintStream = System.out;        //将原来的System.out交给printStream 对象保存  
        ByteArrayOutputStream consoleResult = new ByteArrayOutputStream();   
        System.setOut(new PrintStream(consoleResult));         //设置新的out
        Object scriptResult = null;
        Throwable error = null;
        try {
        	scriptResult = shell.run("println xx 'xxx'", "test", new LinkedList());
		} catch (Throwable t) {
			error = t;
		}
        System.setOut(oldPrintStream);   //恢复原来的System.out  
//		Object result = shell.evaluate("1+1");
        System.out.println("-----------------------------------------");
        System.out.println(consoleResult.toString());     //将console中保存的信息输出 
        System.out.println(error);
		System.out.println(scriptResult);
		
	}
}
